package org.jeecg.modules.project.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.ParamCheckException;
import org.jeecg.modules.project.entity.BimProjectFloor;
import org.jeecg.modules.project.entity.BuildResultType;
import org.jeecg.modules.project.mapper.BimProjectFloorMapper;
import org.jeecg.modules.project.service.IBimProjectFloorService;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: bim_project_floor
 * @Author: jeecg-boot
 * @Date: 2023-04-13
 * @Version: V1.0
 */
@Service
public class BimProjectFloorServiceImpl extends ServiceImpl<BimProjectFloorMapper, BimProjectFloor> implements IBimProjectFloorService {

    @Override
    public List<BimProjectFloor> add(List<BimProjectFloor> bimFloorList) {
        if (CollUtil.isNotEmpty(bimFloorList)) {
            for (BimProjectFloor bimProjectFloor : bimFloorList) {
                if (!isUnique(bimProjectFloor)) {
                    throw new ParamCheckException(MessageFormat.format("已经存在楼栋编码：{0},楼层号:{1}", bimProjectFloor.getBuildId(), bimProjectFloor.getFloorNo()));
                }
            }
            Map<String, List<BimProjectFloor>> buildNoFloorMap = bimFloorList.stream().collect(Collectors.groupingBy(BimProjectFloor::getFloorNo));
            buildNoFloorMap.keySet().stream().forEach(itemKey ->{
                List<BimProjectFloor> buildNoFloors = buildNoFloorMap.get(itemKey);
                if(CollUtil.isNotEmpty(buildNoFloors) && buildNoFloors.size() > 1){
                    throw new ParamCheckException(MessageFormat.format("楼层号重复：{0}", itemKey));
                }
            });
            this.saveBatch(bimFloorList);
        }
        return bimFloorList;
    }

    @Override
    public List<BimProjectFloor> queryFloor(BimProjectFloor floor, BuildResultType resultType) {
        List<BimProjectFloor> floorList = new ArrayList<>();
        LambdaQueryChainWrapper<BimProjectFloor> queryWrapper = new LambdaQueryChainWrapper<>(this.getBaseMapper());
        queryWrapper.eq(ObjectUtil.isNotNull(floor.getFloorNo()), BimProjectFloor::getFloorNo, floor.getFloorNo());
        queryWrapper.eq(ObjectUtil.isNotNull(floor.getBuildId()), BimProjectFloor::getBuildId, floor.getBuildId());
        queryWrapper.eq(ObjectUtil.isNotNull(floor.getFloorId()), BimProjectFloor::getFloorId, floor.getFloorId());
        if (BuildResultType.MAP.getCode().equals(resultType.getCode())) {
            floorList = queryWrapper.select(BimProjectFloor::getFloorId, BimProjectFloor::getBuildId,
                    BimProjectFloor::getFloorNo, BimProjectFloor::getMaps).list();
        } else if (BuildResultType.UNIT.getCode().equals(resultType.getCode())) {
            floorList = queryWrapper.select(BimProjectFloor::getFloorId, BimProjectFloor::getBuildId,
                    BimProjectFloor::getFloorNo).list();
        } else {
            floorList = queryWrapper.select(BimProjectFloor::getFloorId, BimProjectFloor::getBuildId, BimProjectFloor::getFloorNo)
                    .list();
        }
        if(CollUtil.isNotEmpty(floorList)){
            floorList = floorList.stream().sorted((o1, o2) -> {
                if (Integer.valueOf(o1.getFloorNo()) > Integer.valueOf(o2.getFloorNo())) {
                    return 1;
                } else if (Integer.valueOf(o1.getFloorNo()).equals(Integer.valueOf(o2.getFloorNo()))) {
                    return 0;
                } else {
                    return -1;
                }
            }).collect(Collectors.toList());
        }
        return floorList;
    }

    @Override
    public void edit(BimProjectFloor bimProjectFloor) {
        if (!isUnique(bimProjectFloor)) {
            throw new ParamCheckException(MessageFormat.format("已经存在楼栋编码：{0},楼层号:{1}", bimProjectFloor.getBuildId(), bimProjectFloor.getFloorNo()));
        }
        this.updateById(bimProjectFloor);
    }


    public boolean isUnique(BimProjectFloor bimProjectFloor) {
        BimProjectFloor queryFloor = new LambdaQueryChainWrapper<>(this.getBaseMapper())
                .eq(BimProjectFloor::getBuildId, bimProjectFloor.getBuildId())
                .eq(BimProjectFloor::getFloorNo, bimProjectFloor.getFloorNo())
                .list().stream().findFirst().orElse(null);
        return queryFloor == null ? true : false;
    }


}
